package com.itheima.studenthomework.controller;

import com.itheima.studenthomework.domain.User;
import com.itheima.studenthomework.service.UserService;

import java.util.Random;
import java.util.Scanner;

public class RegisterController {
    public void register() {
        Scanner sc = new Scanner(System.in);

        loop:
        while (true) {
            System.out.println("=========欢迎来到黑马管理系统=========");
            System.out.println("1.账号注册");
            System.out.println("2.手机注册");
            System.out.println("3.返回上一级");
            String str = sc.next();

            switch (str){
                case "1":
                    usernameRegister();
                    break;
                case "2":
                    phoneRegister();
                    break;
                case "3":
                    break loop;
                default:
                    System.out.println("您的输入有误，请重新输入！");
                    break;
            }
        }
    }

    private void phoneRegister() {
        Scanner sc = new Scanner(System.in);

        UserService userService = new UserService();
        System.out.println("请输入账号名：");
        String username;
        /*
        一、判断用户名是否存在：
            1.存在：提示并重新输入
            2.不存在：放行；
         */
        loop1:while (true) {
            username = sc.next();
            char[] chr = username.toCharArray();
            for (int i = 0; i < chr.length; i++) {
                if(chr[i] < '0' || chr[i] > '9'){
                    System.out.println("存在非法字符，请重新输入");
                    continue loop1;
                }
            }
            if(chr.length != 11){
                System.out.println("您输入的位数不正确，请重新输入");
                continue;
            }

            int index = userService.isExist(username);
            if(index != -1){
                System.out.println("该用户名已存在，请重新输入");
            }else {
                break;
            }
        }
        /*
        二、密码默认8个0
         */
        String password = "00000000";

        /*
        三、验证码：(必须大于2位)
            1.正确：放行；
            2.错误：重新输入；
         */
        String vcd = verifyCode(6);
        System.out.println(vcd);
        System.out.println("请输入6位验证码：");
        String verifyCode;
        while (true){
            verifyCode = sc.next();
            if(vcd.equalsIgnoreCase(verifyCode)){
                break;
            }else{
                System.out.println("验证码输入错误，请重新输入！");
            }
        }
        /*
        四、传入数据
         */
        User user = new User(username,password);
        boolean flag = userService.addUser(user);
        if(flag){
            System.out.println("注册成功！");
        }else {
            System.out.println("注册失败！");
        }
    }

    private void usernameRegister() {
        Scanner sc = new Scanner(System.in);

        UserService userService = new UserService();
        System.out.println("请输入账号名：");
        String username;
        /*
        一、判断用户名是否存在：
            1.存在：进行密码输入
            2.不存在：重复，直到用户名不重复
         */
        while (true) {
            username = sc.next();
            int index = userService.isExist(username);
            if(index != -1){
                System.out.println("该用户名已存在，请重新输入");
            }else {
                break;
            }
        }
        System.out.println("请输入密码：");
        String password = sc.next();
        /*
        二、密码二次验证：
            1.正确：放行；
            2.错误：重新输入；
         */
        System.out.println("请再次输入密码：");
        String psd;
        while (true) {
            psd = sc.next();
            if(password.equals(psd)){
                break;
            }else{
                System.out.println("输入错误，请重新输入");
            }
        }
        /*
        三、验证码：(必须大于2位)
            1.正确：放行；
            2.错误：重新输入；
         */
        String vcd = verifyCode(4);
        System.out.println(vcd);
        System.out.println("请输入4位验证码：");
        String verifyCode;
        while (true){
            verifyCode = sc.next();
            if(vcd.equalsIgnoreCase(verifyCode)){
                break;
            }else{
                System.out.println("验证码输入错误，请重新输入！");
            }
        }
        /*
        四、传入数据
         */
        User user = new User(username,password);
        boolean flag = userService.addUser(user);
        if(flag){
            System.out.println("注册成功！");
        }else {
            System.out.println("注册失败！");
        }
    }

    public static String verifyCode(int num){
        StringBuilder sb = new StringBuilder("");
        Random random = new Random();

        char[] chr = new char[num];
        while (true) {
            int numCount = 0;
            int smallCount = 0;
            int bigCount = 0;
            for (int i = 0; i < num; i++) {
                int ran = random.nextInt(3);
                switch (ran){
                    case 0:
                        chr[i] = (char) ('0' + random.nextInt(10));
                        numCount ++;
                        break;
                    case 1:
                        chr[i] = (char) ('a' + random.nextInt(26));
                        smallCount ++;
                        break;
                    case 2:
                        chr[i] = (char) ('A' + random.nextInt(26));
                        bigCount ++;
                        break;
                }
            }
            //判定是否有大小写和数字
            if(numCount!=0 && smallCount!=0 && bigCount!=0){
                break;
            }
        }
        for (int i = 0; i < chr.length; i++) {
            sb.append(chr[i]);
        }
        return sb.toString();
    }

    public static void registerOrNotUser(){
        Scanner sc = new Scanner(System.in);
        loop:
        while (true) {
            System.out.println("账号不存在，请问是否注册！");
            System.out.println("1.是");
            System.out.println("2.否");
            String choice = sc.next();
            switch (choice){
                case "1":
                    RegisterController registerController = new RegisterController();
                    registerController.usernameRegister();
                    break loop;
                case "2":
                    break loop;
                default:
                    System.out.println("您的输入有误，请重新输入！");
                    break;
            }
        }
    }

    public static void registerOrNotPhone(){
        Scanner sc = new Scanner(System.in);
        loop:
        while (true) {
            System.out.println("账号不存在，请问是否注册！");
            System.out.println("1.是");
            System.out.println("2.否");
            String choice = sc.next();
            switch (choice){
                case "1":
                    RegisterController registerController = new RegisterController();
                    registerController.phoneRegister();
                    break loop;
                case "2":
                    break loop;
                default:
                    System.out.println("您的输入有误，请重新输入！");
                    break;
            }
        }
    }
}
